package net.xdclass.onlinexdclass.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.onlinexdclass.enums.VideoOrderStateEnum;
import net.xdclass.onlinexdclass.mapper.*;
import net.xdclass.onlinexdclass.model.entity.Episode;
import net.xdclass.onlinexdclass.model.entity.PlayRecord;
import net.xdclass.onlinexdclass.model.entity.Video;
import net.xdclass.onlinexdclass.model.entity.VideoOrder;
import net.xdclass.onlinexdclass.preassert.PreAssert;
import net.xdclass.onlinexdclass.service.VideoOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class VideoOrderServiceImpl implements VideoOrderService {


    @Autowired
    private VideoOrderMapper videoOrderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private EpiSodeMapper epiSodeMapper;

    @Autowired
    private PlayRecordMapper playRecordMapper;

    /**
     * 下单操作
     * 未来版本，优惠券抵扣，风控用户检查，生成订单基础信息，生成支付信息
     *
     * @param userId
     * @param videoId
     * @return
     */
    @Override
    @Transient
    public Integer save(Integer userId, Integer videoId) {

        //判断是否已经购买，如果已经购买，不能重复购买
        VideoOrder orderByUserIdAndVideoIdAndState = videoOrderMapper.findOrderByUserIdAndVideoIdAndState(userId, videoId, VideoOrderStateEnum.PAY_SUCCESS.getState());

        if (null != orderByUserIdAndVideoIdAndState) {
            //表示以购买
            return null;
        }

        //查询video的信息
        Video video = videoMapper.findVideoById(videoId);

        VideoOrder videoOrder = new VideoOrder();
        videoOrder.setCreateTime(new Date());
        videoOrder.setOutTradeNo(UUID.randomUUID().toString());
        videoOrder.setState(VideoOrderStateEnum.PAY_SUCCESS.getState());
        videoOrder.setTotalFee(video.getPrice());
        videoOrder.setUserId(userId);
        videoOrder.setVideoId(videoId);
        videoOrder.setVideoImg(video.getCoverImg());
        videoOrder.setVideoTitle(video.getTitle());

        int saveOrder = videoOrderMapper.saveOrder(videoOrder);


        //生成播放记录
        if (saveOrder == 1) {
            Episode firstEpisodeByVideoId = epiSodeMapper.findFirstEpisodeByVideoId(videoId);
//            if(null == firstEpisodeByVideoId) {
//                throw new XDException(0, "没有集信息，请运营人员检查");
//            }
            //断言工具使用
            PreAssert.notNull(firstEpisodeByVideoId, "没有集信息,请运营人员检查");
            PlayRecord playRecord = new PlayRecord();
            playRecord.setCreateTime(new Date());
            playRecord.setEpisodeId(firstEpisodeByVideoId.getId());
            playRecord.setCurrentNum(firstEpisodeByVideoId.getNum());
            playRecord.setUserId(userId);
            playRecord.setVideoId(videoId);

            playRecordMapper.savePlayRecord(playRecord);
            log.info("保存成功");

        }


        //使用mybatis获取新插入数据的id
        Integer id = videoOrder.getId();
        log.info("新生成订单的id为{}", id);
        return saveOrder;
    }

    @Override
    public List<VideoOrder> listOrderByUserId(Integer user_id) {

        List<VideoOrder> videoOrders = videoOrderMapper.listOrderByUserId(user_id);

        if (CollectionUtils.isEmpty(videoOrders)) {
            return null;
        }

        return videoOrders;
    }
}
